package com.lyrcsoft.springbootaop.d1;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author fujc2dev@126.com
 * @date 2024-10-25
 */
public class ProxyFactory {
    //维护目标对象
    private static Object target;

    //维护关键点代码的类
    private static Transaction aop;
    public static Object getProxyInstance(Object target_) {

        //目标对象和关键点代码的类都是通过外界传递进来
        target = target_;
        aop = new Transaction();

        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                        aop.begin();
                        Object returnValue = method.invoke(target, args);
                        aop.submit();

                        return returnValue;
                    }
                }
        );
    }
}